package com.yixun.qfbao.api.service.impl;

import com.yixun.common.page.Page;
import com.yixun.common.page.PageUtil;
import com.yixun.qfbao.api.service.ChannelRoleService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.mapper.ChannelRoleMapper;
import com.yixun.qfbao.model.ChannelRole;
import com.yixun.qfbao.model.dto.ChannelRoleDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @author guifang.weng
 * @create 2017-11-22 10:25
 **/
@Service
public class ChannelRoleServiceImpl implements ChannelRoleService {

    @Autowired
    private ChannelRoleMapper channelRoleMapper;

    @Override
    public GetChannelRoleListResDto getChannelRoleList(GetChannelRoleListReqDto reqDto) throws Exception {
        GetChannelRoleListResDto resDto = new GetChannelRoleListResDto();
        ChannelRoleDto dto = new ChannelRoleDto();
        dto.setMerchantId(reqDto.getMerchantId());
        List<ChannelRole> channelRoleList = channelRoleMapper.selectListByDto(dto);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setChannelRoleList(channelRoleList);
        return resDto;
    }

    @Override
    public GetChannelRolePageResDto getChannelRolePage(GetChannelRolePageReqDto getChannelRolePageReqDto) throws Exception {
        GetChannelRolePageResDto resDto = new GetChannelRolePageResDto();
        //获取角色分页信息
        ChannelRoleDto channelRoleDto = new ChannelRoleDto();
        channelRoleDto.setRoleName(getChannelRolePageReqDto.getRoleName());
        channelRoleDto.setRoleOrigin(String.valueOf(getChannelRolePageReqDto.getRoleOrigin()));
        channelRoleDto.setMerchantId(getChannelRolePageReqDto.getMerchantId());
        channelRoleDto.setOrders("create_time desc");
        Integer pageNo = getChannelRolePageReqDto.getPageNo();
        Integer pageSize = getChannelRolePageReqDto.getPageSize();
        Integer total = channelRoleMapper.count(channelRoleDto);
        List<ChannelRole> channelRoleList = channelRoleMapper.list(channelRoleDto, PageUtil.begin(pageNo, pageSize), pageSize);
        //设置分页信息
        Page<ChannelRole> channelRoleListPage = new Page<ChannelRole>(total, channelRoleList);
        channelRoleListPage.setPageInfo(total, PageUtil.begin(pageNo, pageSize), pageSize * pageNo);

        //返回结果信息
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setChannelRoleListPage(channelRoleListPage);
        return resDto;
    }

    @Override
    public GetRoleByIdResDto getRoleById(GetRoleByIdReqDto getRoleByIdReqDto) throws Exception {
        GetRoleByIdResDto resDto = new GetRoleByIdResDto();
        ChannelRoleDto channelRoleDto = new ChannelRoleDto();
        channelRoleDto.setId(getRoleByIdReqDto.getId());
        ChannelRole channelRole = channelRoleMapper.selectByDto(channelRoleDto);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setChannelRole(channelRole);
        return resDto;
    }

    @Override
    public AddChannelRoleResDto addChannelRole(AddChannelRoleReqDto addChannelRoleReqDto) throws Exception {
        AddChannelRoleResDto resDto = new AddChannelRoleResDto();
        ChannelRole channelRole = new ChannelRole();
        channelRole.setRoleOrigin(addChannelRoleReqDto.getRoleOrigin());
        channelRole.setRoleName(addChannelRoleReqDto.getRoleName());
        channelRole.setRoleIndexPage(addChannelRoleReqDto.getRoleIndexPage());
        channelRole.setRemark(addChannelRoleReqDto.getRemark());
        channelRole.setCreateUserId(Long.valueOf(addChannelRoleReqDto.getUserId()));
        channelRole.setMerchantId(addChannelRoleReqDto.getMerchantId());
        channelRole.setCreateTime(new Date());
        channelRole.setDelFlag(DelFlagEnum.UNDEL.getNo());
        if (channelRoleMapper.add(channelRole) <= 0) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("新增角色失败");
        } else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("新增角色成功");
        }
        return resDto;
    }

    @Override
    public EditChannelRoleResDto editChannelRole(EditChannelRoleReqDto editChannelRoleReqDto) throws Exception {
        EditChannelRoleResDto resDto = new EditChannelRoleResDto();
        ChannelRole channelRole = new ChannelRole();
        channelRole.setId(editChannelRoleReqDto.getId());
        channelRole.setRemark(editChannelRoleReqDto.getRemark());
        channelRole.setRoleIndexPage(editChannelRoleReqDto.getRoleIndexPage());
        channelRole.setRoleName(editChannelRoleReqDto.getRoleName());
        channelRole.setUpdateUserId(Long.valueOf(editChannelRoleReqDto.getUserId()));
        channelRole.setUpdateTime(new Date());
        if (channelRoleMapper.edit(channelRole) <= 0) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("编辑角色失败");
        } else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("编辑角色成功");
        }
        return resDto;
    }

    @Override
    public DelChannelRoleResDto delChannelRole(DelChannelRoleReqDto delChannelRoleReqDto) throws Exception {
        DelChannelRoleResDto resDto = new DelChannelRoleResDto();
        ChannelRoleDto channelRoleDto = new ChannelRoleDto();
        channelRoleDto.setId(delChannelRoleReqDto.getId());
        if (channelRoleMapper.del(channelRoleDto) <= 0) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("删除角色失败");
        } else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("删除角色成功");
        }
        return resDto;
    }

}
